# -*- coding: utf-8 -*-

class ComparableWave:
  """represents single comparable_wave."""
  def __init__(self, wave_id, wave_title, tag_indexes, structure):
    self.wave_id = wave_id
    self.wave_title = wave_title
    self.tag_indexes = tag_indexes
    self.structure = structure

  def __unicode__(self):  
    return self.wave_title
      
  def __cmp__(self, other):
    begin = True
    for index in range(len(self.tag_indexes)):
      length = len(self.structure[index])
      if self.tag_indexes[index] != other.tag_indexes[index] and self.tag_indexes[index] < length and other.tag_indexes[index] < length:
        return cmp(self.tag_indexes[index], other.tag_indexes[index]) #common values
      elif self.tag_indexes[index] > other.tag_indexes[index]:
        if begin:
          return 1
        return -1
      elif self.tag_indexes[index] < other.tag_indexes[index]:
        if begin:
          return -1
        return 1
      if self.tag_indexes[index] != length and self.tag_indexes[index] == other.tag_indexes[index]:
        begin = False
    return cmp(self.wave_title, other.wave_title) 
    
  @property 
  def end(self):
    if self.tag_indexes == []:
        return -1
    tmp = range(len(self.tag_indexes))
    tmp.reverse()
    for i in tmp:
      if self.tag_indexes[i] < len(self.structure[i]):
        return i
    return 0
        
  @property 
  def begin(self):  
    if self.tag_indexes == []:
        return -1
    for i in range(len(self.tag_indexes)):
      if self.tag_indexes[i] < len(self.structure[i]):
        return i
    return len(self.tag_indexes)